package ${package};

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.common.CommonUtil;
import com.main.pojo.platform.StateInfo;
import com.main.pojo.platform.Bridge;
import ${beanPackage}.${className};
import ${servicePackage}.${className}Service;
/**
 * 对象控制层
 * @author ${author}
 */
@Controller
public class ${className}Controller {
	@Resource
	private ${className}Service ${paramName}Service;
	/**
	 * 跳转地址
	 */
	@RequestMapping("/${paramName}/show")
	public String toIndex(HttpServletRequest request,Model model){
        model.addAttribute("baseUrl",request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/");
		return "function/Show${className}";
	}
	/**
	 * 获取主页面JSON数据
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/get")
	public Map<String,Object> get(HttpServletRequest request){
		Bridge bridge = new Bridge(request);
		String keyword = request.getParameter("keyword");
		String node = request.getParameter("node");
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		List<${className}> list = ${paramName}Service.get(keyword,node,bridge,start,limit);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("root", list);
		<#if showWay == 'LIST'>
        map.put("total", ${paramName}Service.getCount(keyword,node,bridge));
		<#else>
        map.put("total", list.size());
		</#if>
		return map;
	}
	
	/**
	 *验证页面输入的编号数据库是否已经存在
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/validator")
	public Map<String,Object> validator(HttpServletRequest request) {
		Bridge bridge = new Bridge(request);
		String key = request.getParameter("keyid");//需要验证的字段名
		String value = request.getParameter("value");//需要验证的内容
		Map<String,Object> map = new HashMap<String,Object>();
		StateInfo stateInfo = new StateInfo();
		if(!CommonUtil.isEmpty(key)) {
			stateInfo =  ${paramName}Service.validator(key,value,bridge);
		}
		map.put("success",stateInfo.getFlag());
		map.put("msg",stateInfo.getMsg());
		return map;
	}
	
	/**
	 *保存
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/save")
	public Map<String,Object> save(HttpServletRequest request){
		StateInfo stateInfo = new StateInfo();
		Bridge bridge = new Bridge(request);
		String id = request.getParameter("id");
		String parentid = request.getParameter("father");
		${className} ${paramName} = new ${className}();
		<#list beans as bean>
		<#if bean.type == 'STRING'>
		${paramName}.${bean.setMethodName}(request.getParameter("${bean.dataIndex}"));
		<#elseif bean.type == 'INT'>
		${paramName}.${bean.setMethodName}(Integer.parseInt(CommonUtil.nullToZero(request.getParameter("${bean.dataIndex}"))));
		<#else>
		${paramName}.${bean.setMethodName}(Double.parseDouble(CommonUtil.nullToZero(request.getParameter("${bean.dataIndex}"))));
		</#if>
		</#list>
		if(CommonUtil.isEmpty(id)) {
			stateInfo = ${paramName}Service.add(parentid,${paramName},bridge);
		}else {
			stateInfo = ${paramName}Service.edit(id,${paramName},bridge);
		}
		Map<String,Object> map = new HashMap<>();
		if(stateInfo.getFlag()){
			map.put("success",true);
		}else {
			map.put("success",false);
			map.put("message",stateInfo.getMsg());
		}
		return map;
	}
	
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/del")
	public StateInfo del(HttpServletRequest request){
		return ${paramName}Service.delete(request.getParameter("id"),request.getParameter("parentId"),new Bridge(request));
	}
	
	<#if autoInitFromAccess == true>
	/**
	 *从access 初始化对应表名的内置数据【带年份的则去掉年份字符】
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/autoInitFromAccess")
	public StateInfo autoInitFromAccess(HttpServletRequest request){
		return ${paramName}Service.autoInitFromAccess(new Bridge(request));
	}
	</#if>
	<#if copyLastYearData == true>
	/**
	 *拷贝上年数据
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/copyLastYearData")
	public StateInfo copyLastYearData(HttpServletRequest request){
		return ${paramName}Service.copyLasyYearData(new Bridge(request));
	}
	</#if>
	<#if treeSort == true>
	/**
	 *重新调整树形结构包括排序和父子节点关系
	 */
	@ResponseBody
	@RequestMapping("/${paramName}/treeSort")
	public StateInfo treeSort(HttpServletRequest request){
		StateInfo stateInfo = new StateInfo();
		//TODO
		return stateInfo;
	}
	</#if>
	<#if importData == true>
	/**
	 * 导入Excel数据
	 */
	@ResponseBody
	@RequestMapping(value="/${paramName}/importData", method = RequestMethod.POST)
	public Map<String, Object> importData(HttpServletRequest request,@RequestParam("excelFile") MultipartFile file) {
		return CommonUtil.changeFormResult(${paramName}Service.importData(CommonUtil.uploadFileDeal(file), new Bridge(request)));
	}

    /**
	 * 下载Excel导入模板
	 * @param response
	 * @param model
	 */
	@RequestMapping("/${paramName}/exportTemplate")
	public void exportTemplate(HttpServletResponse response,HttpServletRequest request){
		try {
			String fileName = "EXCEL模板_${jsname}.xls";
			response.setHeader("conent-type", "application/octet-stream");
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
			OutputStream os = response.getOutputStream();
            ${paramName}Service.exportExcelTemplate(os,new Bridge(request));
			if (os != null) {
				os.close();
			}
		} catch (IOException e) {
			Logger.getLogger(this.getClass()).error(e);
		}

	}

    /**
	 * 下载Excel导入模板
	 * @param response
	 * @param model
	 */
	@RequestMapping("/${paramName}/exportExcel")
	public void exportExcel(HttpServletResponse response,HttpServletRequest request){
		try {
			String fileName = "EXCEL数据_${jsname}.xls";
			response.setHeader("conent-type", "application/octet-stream");
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
			OutputStream os = response.getOutputStream();
            ${paramName}Service.exportData(os,new Bridge(request));
			if (os != null) {
				os.close();
			}
		} catch (IOException e) {
			Logger.getLogger(this.getClass()).error(e);
		}

	}
	</#if>
}
